// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MockRobotiqSAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Hardware.Runtime.Robotiq
{
    using System;
    using System.Net.Sockets;
    using System.Runtime.Serialization;
    using Microsoft.Robotics.Hardware.Robotiq;
    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Runtime;

    /// <summary>
    /// Mock Robotiq agent - simply publishes the requested state
    /// </summary>
    [DataContract]
    public class MockRobotiqSAgent :
        ConsumerProducerAgent<TimerMessage, RobotiqSModelStateMessage>,
        IControlReceiver<RobotiqSModelControlMessage>
    {
        /// <summary>
        /// Current state to publish
        /// </summary>
        private RobotiqSModelState currentState;

        /// <summary>
        /// Last requested gripper command
        /// </summary>
        private RobotiqSModelBasicCommand lastRequestedCommand;

        /// <summary>
        /// Initializes a new instance of the <see cref="MockRobotiqSAgent" /> class.
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producer">Producer to subscribe to</param>
        public MockRobotiqSAgent(string name, IProducer<TimerMessage> producer)
            : base(name, producer.Name)
        {
        }

        /// <summary>
        /// Called when the agent is about to start running
        /// </summary>
        public override void OnActivated()
        {
            base.OnActivated();
            this.currentState = new RobotiqSModelState();

            this.currentState.IsActivated = true;
            this.currentState.GoToPosition = true;
            this.currentState.GripStatus = RobotiqSModelGripStatus.Stopped;
            this.currentState.FingerAState = new RobotiqSModelFingerState();
            this.currentState.FingerBState = new RobotiqSModelFingerState();
            this.currentState.FingerCState = new RobotiqSModelFingerState();
        }

        /// <summary>
        /// Called when the agent receives a control message.
        /// </summary>
        /// <param name="message">The received message.</param>
        public void ReceiveControl(RobotiqSModelControlMessage message)
        {
            this.lastRequestedCommand = message.Command;

            this.lastRequestedCommand.GripMode = this.lastRequestedCommand.GripMode;
            this.currentState.FingerAState.CommandedPosition = this.lastRequestedCommand.Position;
            this.currentState.FingerBState.CommandedPosition = this.lastRequestedCommand.Position;
            this.currentState.FingerCState.CommandedPosition = this.lastRequestedCommand.Position;
            this.currentState.FingerAState.Position = this.lastRequestedCommand.Position;
            this.currentState.FingerBState.Position = this.lastRequestedCommand.Position;
            this.currentState.FingerCState.Position = this.lastRequestedCommand.Position;
            this.currentState.FingerAState.FingerStatus = RobotiqSModelFingerStatus.AtRequestedPosition;
            this.currentState.FingerBState.FingerStatus = RobotiqSModelFingerStatus.AtRequestedPosition;
            this.currentState.FingerCState.FingerStatus = RobotiqSModelFingerStatus.AtRequestedPosition;
        }

        /// <summary>
        /// Called when the agent receives a message.
        /// </summary>
        /// <param name="message">The received message.</param>
        public override void Receive(TimerMessage message)
        {
            RobotiqSModelStateMessage stateMessage = new RobotiqSModelStateMessage(message, this.currentState);

            this.Publisher.Post(stateMessage);
        }
    }
}
